package com.mvp.shq.washer.ui.withdraw;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.widget.CustomDialog;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithDrawActivity extends BaseActivity<Presenter, Model> implements WithDrawConstract.View {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.et_realname)
    EditText etRealname;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_balance)
    EditText etBalance;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_balance_all)
    TextView tvBalanceAll;
    @BindView(R.id.btn_withdraw)
    Button btnWithdraw;
    @BindView(R.id.et_account_1)
    EditText etAccount1;
    private BigDecimal money;
    private CustomDialog dialog;
    @Override
    public int getLayoutId() {
        return R.layout.activity_with_draw;
    }

    @Override
    public void initView() {
        money = SpUtil.getUser().getRechargeBalance();
        tvBalance.setText("当前可提现"+money+"元");
    }

    @OnClick({R.id.all_withdraw_rule,R.id.back, R.id.btn_withdraw,R.id.tv_balance_all,R.id.tv_withdraw_rule})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_withdraw:
                String account = etAccount.getText().toString();
                String account1 = etAccount1.getText().toString();
                String m = etBalance.getText().toString();
                if (TextUtils.isEmpty(m) || " ".equals(m)){
                    T.show("请先输入提现金额");
                    return;
                }
                BigDecimal withDrawMoney = new BigDecimal(m);
                String realName = etRealname.getText().toString();
                if (TextUtils.isEmpty(realName) || " ".equals(realName)){
                    T.show("请先输入真实姓名");
                    return;
                }
                if (TextUtils.isEmpty(account) || " ".equals(account)){
                    T.show("请先输入账号信息");
                    return;
                }
                if (TextUtils.isEmpty(account1) || " ".equals(account1)){
                    T.show("请输入确认账号信息");
                    return;
                }
                if (!account.equals(account1)){
                    T.show("请核实两次输入的账号信息");
                    return;
                }
                if (withDrawMoney.compareTo(money) > 0){
                    T.show("提现金额不能比账户余额多");
                    return;
                }
                BigDecimal s = new BigDecimal("0.1");
                if (withDrawMoney.compareTo(s) == -1){
                    T.show("提现金额不能低于2元");
                    return;
                }
                CustomDialog.Builder builder = new CustomDialog.Builder(context);
                dialog = builder.setMessage("提现手续费为2.0元/笔及赠送金额清零，是否确认提现？")
                        .setNegativeButton("确认", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                showLoadingDialog(true);
                                presenter.withDraw(withDrawMoney,realName,account);
                            }
                        })
                        .setPositiveButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        }).createTwoButtonDialog();
                dialog.show();
                break;
            case R.id.tv_balance_all:
                etBalance.setText(money+"");
                break;
            case R.id.all_withdraw_rule:
                JumpUtil.toRule(context,"withDrawRule");
                break;
        }
    }

    @Override
    public void withDrawS() {
        T.show("成功发起提现");
        RxBus.get().send(EventCode.GET_USER_INFO);
        finish();
    }

    @Override
    public void withDrawF(String msg) {
        T.show(msg);
    }
}
